<?php

/*
 * Created on 2009-7-16
 * Author guobin@yizin.net
 */

class FeatureManagerUI
{

    public $version = "0.9.0";

    public $title;

    public $featureManager;

    public function __construct($featureManager, $url, $resourceBase)
    {
        $this->featureManager = $featureManager;
        $this->url = $url;
        $this->resourceBase = $resourceBase;
    }

    public function show()
    {
        $version = $this->version;
        $title = $this->title;
        $featureManager = $this->featureManager;
        $basePath = $this->resourceBase;
        $url = $this->url;
        $preservedFeatures = $this->featureManager->preservedFeatures;


        //dispatch
        $requestURI = $_SERVER['REQUEST_URI'];
        if (strrpos($requestURI, "/") != strlen($requestURI)-1)
        {
            header(sprintf("Location: %s/", $requestURI));
            exit ;
        }

        $URI = "/".substr($requestURI, strlen($url));

        $matches = array ();

        //update extension priority
        preg_match("{^/([^/]+)/([^/]+)/([^/]+)/update/$}", $URI, $matches);
        if ($matches)
        {
            $featureId = $matches[1];
            $extendableId = $matches[2];
            $extensionId = $matches[3];
            $featureManager->updateExtensionPriority($extensionId, $extendableId, $_POST['direction']);
            header(sprintf("Location: %s", $_POST['pre']));
            exit ;
        }

        //reset extendable priorities
        preg_match("{^/([^/]+)/([^/]+)/resetpriority/$}", $URI, $matches);
        if ($matches)
        {
            $featureId = $matches[1];
            $extendableId = $matches[2];
            $featureManager->resetExtendablePriorities($extendableId);
            header(sprintf("Location: %s", $url.$featureId."/".$extendableId."/"));
            exit ;
        }

        //update feature status
        preg_match("{^/([^/]+)/update/$}", $URI, $matches);
        if ($matches)
        {
            $featureId = $matches[1];
            $featureManager->updateFeatureStatus($featureId, $_POST['status']);
            header(sprintf("Location: %s", $_POST['pre']));
            exit ;
        }

        preg_match("{^/(([^/]+)/(([^/]+)/)?)?$}", $URI, $matches);
        if ($matches)
        {
            $featureId = $matches[2];
            $extendableId = $matches[4];
        }



        if (!$featureId)
        {
            $title = 'Features';
            $features = $featureManager->getFeatures();
        }
        elseif (!$extendableId)
        {
            $title = $featureId;
            $features = $featureManager->getFeatures();
            $feature = $features[$featureId];
            $extendables = $featureManager->getExtendablesByFeature($featureId);
        }
        else
        {
            $title = $extendableId;
            $extendables = $featureManager->getExtendablesByFeature($featureId);
            $extendable = $extendables[$extendableId];
            $extensions = $featureManager->getExtensionsByExtendable($extendableId);
        }

?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>
            <?= $title ?>
            - Feature Manager</title>
        <link rel="stylesheet" href="<?= $basePath ?>styles/yizin-fm.css" type="text/css" />
        <link rel="icon" type="image/x-icon" href="<?= $basePath ?>images/favicon.ico" />
        <link rel="shortcut icon" type="image/x-icon" href="<?= $basePath ?>images/favicon.ico" />
    </head>
    <body>
        <div class="top-bar">
            <h1>Feature Manager </h1>
        </div>
        <div class="page-title">
            <h2>
                <?= $title ?>
            </h2>
        </div>
        <div class="tool-bar">
            / <a href="<?=$url?>">home</a>
            / 
            <?php
            if ($featureId)
            {
            ?>
            <a href="<?=$url?><?=$featureId?>">
                <?= $featureId ?>
            </a>/
            <?php
            if ($extendableId)
            {
            ?>
            <a href="<?=$url?><?=$featureId?>/<?=$extendableId?>">
                <?= $extendableId ?>
            </a>/
            <?php
            }
            ?>
            <?php
            }
            ?>
        </div>
        <div class="page-content">
            <?php
            if (!$featureId)
            {
            ?>
            <h3>Features:</h3>
            <form id="statusForm" method="post">
                <input id="statusInput" type="hidden" name="status" /><input id="preInput" type="hidden" name="pre" value="<?=$url?>" />
            </form>
            <table>
                <tr>
                    <th>
                        Id
                    </th>
                    <th>
                        Name
                    </th>
                    <th>
                        Introduction
                    </th>
                    <th>
                        Status
                    </th>
                    <th>
                        Control
                    </th>
                </tr>
                <?php
                foreach ($features as $feature)
                {
                ?>
                <tr>
                    <td>
                        <a href="<?=$url?><?=$feature->id?>">
                            <?= $feature->id ?>
                        </a>
                    </td>
                    <td>
                        <?= $feature->name ?>
                    </td>
                    <td>
                        <pre><?= $feature->introduction ?></pre>
                    </td>
                    <td>
                        <?= $feature->status == "ENABLED"?'<span class="enabled">enabled</span>':($feature->status == "SUSPENDED"?'<span class="suspended">suspended</span>':'<span class="disabled">disabled</span>') ?>
                    </td>
                    <td>
                        <?php
                        if (!in_array($feature->id, $preservedFeatures))
                        {
                        ?>
                        <?= $feature->status != "ENABLED"?'<a href="#" onclick="document.getElementById(\'statusInput\').value=\'ENABLED\'; document.getElementById(\'statusForm\').action=\''.$url.$feature->id.'/update/\'; document.getElementById(\'statusForm\').submit(); "><img src="'.$basePath.'images/place-holder.gif" class="enable-a" alt="" /></a>':'<img src="'.$basePath.'images/place-holder.gif" class="enable-b" alt="" />' ?>
                        <?= $feature->status == "ENABLED"?'<a href="#" onclick="document.getElementById(\'statusInput\').value=\'SUSPENDED\'; document.getElementById(\'statusForm\').action=\''.$url.$feature->id.'/update/\'; document.getElementById(\'statusForm\').submit(); "><img src="'.$basePath.'images/place-holder.gif" class="suspend-a" alt="" /></a>':'<img src="'.$basePath.'images/place-holder.gif" class="suspend-b" alt="" />' ?>
                        <?= $feature->status != "DISABLED"?'<a href="#" onclick="document.getElementById(\'statusInput\').value=\'DISABLED\'; document.getElementById(\'statusForm\').action=\''.$url.$feature->id.'/update/\'; document.getElementById(\'statusForm\').submit(); "><img src="'.$basePath.'images/place-holder.gif" class="disable-a" alt="" /></a>':'<img src="'.$basePath.'images/place-holder.gif" class="disable-b" alt="" />' ?>
                        <?php
                        }
                        else
                        {
                        ?>
						<img src="<?=$basePath?>images/place-holder.gif" class="enable-b" alt="" />
						<img src="<?=$basePath?>images/place-holder.gif" class="suspend-b" alt="" />
						<img src="<?=$basePath?>images/place-holder.gif" class="disable-b" alt="" />
						<?php
                        }
                        ?>
                    </td>
                </tr>
                <?
                }
                ?>
            </table>
            <?php
            }
            elseif (!$extendableId)
            {
            ?>
            <form id="statusForm" method="post">
                <input id="statusInput" type="hidden" name="status" /><input id="preInput" type="hidden" name="pre" value="<?=$url?><?=$featureId?>/" />
            </form>
            <table>
                <tr>
                    <th>
                        Id
                    </th>
                    <th>
                        Name
                    </th>
                    <th>
                        Introduction
                    </th>
                    <th>
                        Status
                    </th>
                    <th>
                        Control
                    </th>
                </tr>
                <tr>
                    <td>
                        <?= $featureId ?>
                    </td>
                    <td>
                        <?= $feature->name ?>
                    </td>
                    <td>
                        <pre><?= $feature->introduction ?></pre>
                    </td>
                    <td>
                        <?= $feature->status == "ENABLED"?'<span class="enabled">enabled</span>':($feature->status == "SUSPENDED"?'<span class="suspended">suspended</span>':'<span class="disabled">disabled</span>') ?>
                    </td>
                    <td>
                        <?= $feature->status != "ENABLED"?'<a href="#" onclick="document.getElementById(\'statusInput\').value=\'ENABLED\'; document.getElementById(\'statusForm\').action=\''.$url.$featureId.'/update/\'; document.getElementById(\'statusForm\').submit(); "><img src="'.$basePath.'images/place-holder.gif" class="enable-a" alt="" /></a>':'<img src="'.$basePath.'images/place-holder.gif" class="enable-b" alt="" />' ?>
                        <?= $feature->status == "ENABLED"?'<a href="#" onclick="document.getElementById(\'statusInput\').value=\'SUSPENDED\'; document.getElementById(\'statusForm\').action=\''.$url.$featureId.'/update/\'; document.getElementById(\'statusForm\').submit(); "><img src="'.$basePath.'images/place-holder.gif" class="suspend-a" alt="" /></a>':'<img src="'.$basePath.'images/place-holder.gif" class="suspend-b" alt="" />' ?>
                        <?= $feature->status != "DISABLED"?'<a href="#" onclick="document.getElementById(\'statusInput\').value=\'DISABLED\'; document.getElementById(\'statusForm\').action=\''.$url.$featureId.'/update/\'; document.getElementById(\'statusForm\').submit(); "><img src="'.$basePath.'images/place-holder.gif" class="disable-a" alt="" /></a>':'<img src="'.$basePath.'images/place-holder.gif" class="disable-b" alt="" />' ?>
                    </td>
                </tr>
            </table>
            <h3>Extendables:</h3>
            <table>
                <tr>
                    <th>
                        Id
                    </th>
                    <th>
                        Introduction
                    </th>
                </tr>
                <?php
                foreach ($extendables as $extendable)
                {
                ?>
                <tr>
                    <td>
                        <a href="<?=$url?><?=$featureId?>/<?=$extendable->id?>">
                            <?= $extendable->id ?>
                        </a>
                    </td>
                    <td>
                        <pre><?= $extendable->introduction ?></pre>
                    </td>
                </tr>
                <?
                }
                ?>
            </table>
            <?php
            }
            else
            {
            ?>
            <form id="directionForm" method="post">
                <input id="directionInput" type="hidden" name="direction" /><input id="preInput" type="hidden" name="pre" value="<?=$url?><?=$featureId?>/<?=$extendableId?>/" />
            </form>
            <table>
                <tr>
                    <th>
                        Id
                    </th>
                    <th>
                        Introduction
                    </th>
                </tr>
                <tr>
                    <td>
                        <?= $extendableId ?>
                    </td>
                    <td>
                        <pre><?= $extendable->introduction ?></pre>
                    </td>
                </tr>
            </table>
            <h3>Extensions:</h3>
            <table>
                <tr>
                    <th>
                        Id
                    </th>
                    <th>
                        Name
                    </th>
                    <th>
                        Introduction
                    </th>
                    <th>
                        Priority <span class="escape">(<a href="<?=$url?><?=$featureId?>/<?=$extendableId?>/resetpriority/">reset</a>)</span>
                    </th>
                    <th>
                        Providing Feature
                    </th>
                    <th>
                        Control
                    </th>
                </tr>
                <?php
                $count = count($extensions);
                $i = 0;
                foreach ($extensions as $extension)
                {
                    $i++;
                ?>
                <tr>
                    <td>
                    <?= $extension->id ?>
                    </a>
                </td>
                <td>
                    <?= $extension->name ?>
                </td>
                <td>
                    <pre><?= $extension->introduction ?></pre>
                </td>
                <td>
                    <?= $extension->priority ?>
                    <?= isset ($extension->originalPriority)?"($extension->originalPriority)":"" ?>
                </td>
                <td>
                    <a href="<?=$url?><?=$extension->feature->id?>">
                        <?= $extension->feature->id ?>
                    </a>
                </td>
                <td>
                    <?= $i != 1?'<a href="#" onclick="document.getElementById(\'directionInput\').value=\'up\'; document.getElementById(\'directionForm\').action=\''.$url.$featureId.'/'.$extendableId.'/'.$extension->id.'/update/\'; document.getElementById(\'directionForm\').submit(); "><img src="'.$basePath.'images/place-holder.gif" class="up-a" alt="" /></a>':'<img src="'.$basePath.'images/place-holder.gif" class="up-b" alt="" />' ?>
                    <?= $i != $count?'<a href="#" onclick="document.getElementById(\'directionInput\').value=\'down\'; document.getElementById(\'directionForm\').action=\''.$url.$featureId.'/'.$extendableId.'/'.$extension->id.'/update/\'; document.getElementById(\'directionForm\').submit(); "><img src="'.$basePath.'images/place-holder.gif" class="down-a" alt="" /></a>':'<img src="'.$basePath.'images/place-holder.gif" class="down-b" alt="" />' ?>
                </td>
                </tr>
                <?
                }
                ?>
            </table>
            <?php
            }
            ?>
        </div>
        <div class="page-footer">
            Feature Manager version
            <?= $version ?>
            &copy; Yizin Inc. 
        </div>
    </body>
</html>
<?php
}

}
?>
